package com.dwd.org.service.impl;

import com.dwd.basic.constant.PetLoginConstant;
import com.dwd.basic.service.impl.BaseServiceImpl;
import com.dwd.basic.utils.MD5Utils;
import com.dwd.basic.utils.StrUtils;
import com.dwd.org.mapper.EmployeeMapper;
import com.dwd.org.mapper.ShopMapper;
import com.dwd.org.pojo.Employee;
import com.dwd.org.pojo.Shop;
import com.dwd.org.service.IShopService;
import com.dwd.user.mapper.LoginInfoMapper;
import com.dwd.user.pojo.LoginInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/15 11:00
 * @Version 1.0
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settledIn(Shop shop) {
        //存储商家信息 返回主键,没有admin_id
        super.save(shop);
        //获取员工信息
        Employee admin = shop.getAdmin();
        //设置对应的商家,设置对应得shop_id，因为刚刚存储商家得时候，已经有了主键

        //设置盐值
        admin.setSalt(StrUtils.getComplexRandomString(10));
        //设置密码
        admin.setPassword(MD5Utils.encrypByMd5(admin.getPassword() + admin.getSalt()));
        //创建loginInfo对象
        LoginInfo loginInfo = createLoginInfo(admin);
        loginInfoMapper.save(loginInfo);

        //设置loginInfo
        admin.setLoginInfo(loginInfo);
        admin.setShop(shop);
        //得到主键id也就是，shop对应得admin_id
        employeeMapper.save(admin);
        //更新，这时候admin已经有了id，所以可以更新admin_id
        super.update(shop);
    }

    private LoginInfo createLoginInfo(Employee admin) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(admin,loginInfo);
        loginInfo.setType(PetLoginConstant.ADMIN);
        return loginInfo;
    }

    /**
     * 通过
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id) {
        shopMapper.updateById(id);
    }

    /**
     * 驳回
     *
     * @param shop_id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTurnById(Long shop_id) {
        shopMapper.updateTurnById(shop_id);
    }

    /**
     * 查询邮箱
     *
     * @param id
     * @return
     */
    @Override
    public String queryEmailById(Long id) {
        return shopMapper.queryEmailById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePassById(Long id) {
        shopMapper.updatePassById(id);
    }

    /**
     * 修改商家入驻
     *
     * @param shop
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSettlement(Shop shop) {
        shopMapper.updateShop(shop);
        Long adminId = shopMapper.queryAdminIdById(shop.getId());
        Employee admin = shop.getAdmin();
        String username = admin.getUsername();
        String email = admin.getEmail();
        String phone = admin.getPhone();
        Long loginInfoId = admin.getLoginInfo().getId();
        //修改logininfo
        loginInfoMapper.updateLoginInfo(username,email,phone,loginInfoId);

        shopMapper.updateAdmin(admin, adminId);
    }

    /**
     * 批量通过
     *
     * @param shops
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(List<Shop> shops) {
        shopMapper.batchUpdate(shops);
    }
}
